package com.uw.uwrpc.core.config;

import com.uw.uwrpc.core.api.RegistryCenter;
import com.uw.uwrpc.core.api.TrafficControl;
import com.uw.uwrpc.core.provider.ProviderBootstrap;
import com.uw.uwrpc.core.provider.ProviderInvoker;
import com.uw.uwrpc.core.provider.invoker.ReflectProviderInvoker;
import com.uw.uwrpc.core.registry.impl.UWRegistryCenter;
import com.uw.uwrpc.core.traffic.TimeWindowTrafficControl;
import com.uw.uwrpc.core.transport.SpringBootTransport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

@SpringBootConfiguration
@Import(SpringBootTransport.class)
@EnableConfigurationProperties({UWRpcProperty.class, UWRegistryProperty.class})
public class ProviderConfig {

    @Bean
    public ProviderBootstrap providerBootstrap() {
        return new ProviderBootstrap();
    }

    @Bean
    public RegistryCenter registryCenter(@Autowired UWRegistryProperty uwRegistryProperty) {
        return new UWRegistryCenter(uwRegistryProperty);
    }

    @Bean
    public ProviderInvoker providerInvoker(ProviderBootstrap providerBootstrap) {
        return new ReflectProviderInvoker(providerBootstrap);
    }

    @Bean
    public TrafficControl trafficControl(@Autowired UWRpcProperty uwRpcProperty) {
        return new TimeWindowTrafficControl(uwRpcProperty);
    }

    @Bean
    @ConditionalOnMissingBean
    public ApolloChangedListener provider_apolloChangedListener() {
        return new ApolloChangedListener();
    }
}
